#include "RemoteMediaStream.h"
#include "ConnectionFactory.h"
#include <QDebug>
#include "LocalMediaStream.h"
#include "JanusPeerConnection.h"

RemoteMediaStream::RemoteMediaStream(const qint64& peerID, QObject *parent) : QObject(parent)
{
    m_peerID = peerID;
    m_peerConnection = nullptr;
    m_pJanusPeerConnection = nullptr;
}

RemoteMediaStream::~RemoteMediaStream()
{
}

void RemoteMediaStream::setPeerConnection(JanusPeerConnection *peer)
{
    m_pJanusPeerConnection = peer;
}

void RemoteMediaStream::connectToPeer(const bool &dtls)
{
    rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> inter = ConnectionFactory::get();

    webrtc::PeerConnectionInterface::RTCConfiguration config;
    config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
    config.enable_dtls_srtp = dtls;
    webrtc::PeerConnectionInterface::IceServer server;
    server.uri = "stun:stun.l.google.com:19302";
    config.servers.push_back(server);

    m_peerConnection = inter->CreatePeerConnection(
        config, nullptr, nullptr, this);

    if(m_peerConnection->GetSenders().empty())
    {
        m_peerConnection->AddTrack(LocalMediaStream::getInstance()->getAudioTrack(), {LABEL_AUDIO});
        m_peerConnection->AddTrack(LocalMediaStream::getInstance()->getVideoTrack(), {LABEL_VIDEO});
    }
    // 上面代码需要做判断
    m_peerConnection->CreateOffer(this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
}

void RemoteMediaStream::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)
{
    Q_UNUSED(new_state);
    qDebug() << __func__;
}

void RemoteMediaStream::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
{
    Q_UNUSED(stream);
    qDebug() << __func__;
}

void RemoteMediaStream::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
{
    Q_UNUSED(stream);
    qDebug() << __func__;
}

void RemoteMediaStream::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
{
    Q_UNUSED(data_channel);
    qDebug() << __func__;
}

void RemoteMediaStream::OnRenegotiationNeeded()
{
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)
{
    Q_UNUSED(new_state);
    qDebug() << __func__;
}

void RemoteMediaStream::OnStandardizedIceConnectionChange(
        webrtc::PeerConnectionInterface::IceConnectionState new_state)
{
    Q_UNUSED(new_state);
    qDebug() << __func__;
}

void RemoteMediaStream::OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state)
{
    Q_UNUSED(new_state);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)
{
    Q_UNUSED(new_state);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceCandidate(const webrtc::IceCandidateInterface *candidate)
{
    Q_UNUSED(candidate);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceCandidateError(
        const std::string &host_candidate,
        const std::string &url,
        int error_code,
        const std::string &error_text)
{
    Q_UNUSED(host_candidate);
    Q_UNUSED(url);
    Q_UNUSED(error_code);
    Q_UNUSED(error_text);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceCandidateError(
        const std::string &address,
        int port,
        const std::string &url,
        int error_code,
        const std::string &error_text)
{
    Q_UNUSED(address);
    Q_UNUSED(port);
    Q_UNUSED(url);
    Q_UNUSED(error_code);
    Q_UNUSED(error_text);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceCandidatesRemoved(const std::vector<cricket::Candidate> &candidates)
{
    Q_UNUSED(candidates);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceConnectionReceivingChange(bool receiving)
{
    Q_UNUSED(receiving);
    qDebug() << __func__;
}

void RemoteMediaStream::OnIceSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent &event)
{
    Q_UNUSED(event);
    qDebug() << __func__;
}

void RemoteMediaStream::OnAddTrack(
        rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
        const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface> > &streams)
{
    Q_UNUSED(receiver);
    Q_UNUSED(streams);
    qDebug() << __func__;
}

void RemoteMediaStream::OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)
{
    Q_UNUSED(transceiver);
    qDebug() << __func__;
}

void RemoteMediaStream::OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
{
    Q_UNUSED(receiver);
    qDebug() << __func__;
}

void RemoteMediaStream::OnInterestingUsage(int usage_pattern)
{
    Q_UNUSED(usage_pattern);
    qDebug() << __func__;
}

void RemoteMediaStream::OnSuccess(webrtc::SessionDescriptionInterface *desc)
{
    Q_UNUSED(desc);
    qDebug() << __func__;
    m_peerConnection->SetLocalDescription(DummySetSessionDescriptionObserver::Create(), desc);

    std::string strSdp;
    desc->ToString(&strSdp);

    if(m_pJanusPeerConnection != nullptr)
    {
        m_pJanusPeerConnection->sendSDP(QString::fromStdString(strSdp), QString::fromStdString(desc->type()));
    }
}

void RemoteMediaStream::OnFailure(webrtc::RTCError error)
{
    Q_UNUSED(error);
    qDebug() << __func__;
}
